package com.java.escape.NullPointException;

import java.util.Optional;

/**
 * @ClassName OptionalUtils
 * @Description TODO
 * @Author wyp
 * @Date 2023/8/22 15:46
 * @Version 1.0
 */
public class OptionalUtils {
    public static class User {
        private String name;
        private Integer age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

    public static void main(String[] args) {

        User user = new User();
        user.setAge(1);
        User user1 = null;
        System.out.println(Optional.ofNullable(user).orElse(new User()));
        System.out.println(Optional.ofNullable(user1).orElse(user));
        System.out.println(Optional.ofNullable(user).filter(i -> i.getAge() != 1).orElseThrow(NullPointerException::new));
    }
}
